using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Appr;
using net_work.Data.Enum.Appr;

namespace net_work.Config.DataBaseConfig.Appr;

/// <summary>
/// 审批实例实体配置类
/// </summary>
public class ApprovalInstanceEntityTypeConfiguration : IEntityTypeConfiguration<ApprovalInstance>
{
    public void Configure(EntityTypeBuilder<ApprovalInstance> builder)
    {
        builder.ToTable("appr_inst");

        builder.HasKey(ai => ai.Id);

        // 配置字段名映射
        builder.Property(ai => ai.Id).HasColumnName("inst_id");
        builder.Property(ai => ai.Code).HasColumnName("inst_code").IsRequired().HasMaxLength(64);
        builder.Property(ai => ai.BeginTime).HasColumnName("inst_begin_time");
        builder.Property(ai => ai.EndTime).HasColumnName("inst_end_time");

        // 将 InstanceStatus 枚举存储为字符串
        builder.Property(ai => ai.Status)
            .HasColumnName("inst_status")
            .IsRequired()
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(),
                v => (InstanceStatus)Enum.Parse(typeof(InstanceStatus), v)
            );

        builder.Property(ai => ai.FlowId).HasColumnName("flow_id").IsRequired();
        builder.Property(ai => ai.CurrentFlowNodeId).HasColumnName("flow_node_cur_id");
        builder.Property(ai => ai.BusinessId).HasColumnName("bns_id").IsRequired();

        builder.Property(ai => ai.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(ai => ai.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(ai => ai.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(ai => ai.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(ai => ai.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(ai => ai.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}